package cn.iocoder.yudao.module.ao.controller.admin.school;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.school.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.school.SchoolDO;
import cn.iocoder.yudao.module.ao.service.school.SchoolService;

@Tag(name = "管理后台 - 学校信息")
@RestController
@RequestMapping("/ao/school")
@Validated
public class SchoolController {

    @Resource
    private SchoolService schoolService;

    @PostMapping("/create")
    @Operation(summary = "创建学校信息")
    @PreAuthorize("@ss.hasPermission('ao:school:create')")
    public CommonResult<Long> createSchool(@Valid @RequestBody SchoolSaveReqVO createReqVO) {
        return success(schoolService.createSchool(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学校信息")
    @PreAuthorize("@ss.hasPermission('ao:school:update')")
    public CommonResult<Boolean> updateSchool(@Valid @RequestBody SchoolSaveReqVO updateReqVO) {
        schoolService.updateSchool(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学校信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ao:school:delete')")
    public CommonResult<Boolean> deleteSchool(@RequestParam("id") Long id) {
        schoolService.deleteSchool(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除学校信息")
                @PreAuthorize("@ss.hasPermission('ao:school:delete')")
    public CommonResult<Boolean> deleteSchoolList(@RequestParam("ids") List<Long> ids) {
        schoolService.deleteSchoolListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学校信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:school:query')")
    public CommonResult<SchoolRespVO> getSchool(@RequestParam("id") Long id) {
        SchoolDO school = schoolService.getSchool(id);
        return success(BeanUtils.toBean(school, SchoolRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学校信息分页")
    @PreAuthorize("@ss.hasPermission('ao:school:query')")
    public CommonResult<PageResult<SchoolRespVO>> getSchoolPage(@Valid SchoolPageReqVO pageReqVO) {
        PageResult<SchoolDO> pageResult = schoolService.getSchoolPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchoolRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学校信息 Excel")
    @PreAuthorize("@ss.hasPermission('ao:school:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSchoolExcel(@Valid SchoolPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchoolDO> list = schoolService.getSchoolPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学校信息.xls", "数据", SchoolRespVO.class,
                        BeanUtils.toBean(list, SchoolRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得学校简单列表")
    @PreAuthorize("@ss.hasPermission('ao:school:query')")
    public CommonResult<List<SchoolRespVO>> getSimpleSchoolList() {
        List<SchoolDO> list = schoolService.getSimpleSchoolList();
        return success(BeanUtils.toBean(list, SchoolRespVO.class));
    }

}